package cp.remote.timer;

import android.os.Handler;
import cp.remote.activity.Main_Activity;
import cp.remote.network.Worker;

public class CampusBotNetworkTimer implements Runnable {

    private Worker w = null;
    private Main_Activity act = null;
    private Handler h;

    public static void startHandler(Main_Activity act, Worker w){
        Handler h = new Handler();
        CampusBotNetworkTimer t = new CampusBotNetworkTimer(act, w, h);
        h.removeCallbacks(t);
        h.postAtTime(t, 100);
    }
    
    private CampusBotNetworkTimer(Main_Activity act, Worker w, Handler h){
        this.act = act;
        this.w = w;
        this.h = h;
    }

    public void run() {
        if (w.isLastCommandOk() || w.isError()) {
            String t = w.getName() + ": ";
            if (w.isError()) {
                act.setStateText(t + w.getErrormsg() + "\n");                
            } else {
                act.setStateText("Command OK");          
            }
        }else{
            h.postDelayed(this, 50);
        }
    }
}
